package com.hst.capacity.infra.persistence.jpa.marketing.promotion;


import com.hst.capacity.domain.model.entity.marketing.promotion.PromotionEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDateTime;
import java.util.List;

public interface PromotionRepository extends JpaRepository<PromotionEntity, String> {

    @Query("select t from PromotionEntity t where t.idPromotion = :idPromotion")
    List<PromotionEntity> findByIdPromotion(String idPromotion);

    @Override
    List<PromotionEntity> findAllById(Iterable<String> strings);

    /**
     * todo 条件限定
     * @param idFranchisee
     * @return
     */
    @Query("select t from PromotionEntity t where t.idFranchisee=:idFranchisee and t.enabled =1 and :today between t.effectiveTime and t.expiredTime ORDER BY t.createTime DESC")
    List<PromotionEntity> findByIdFranchisee(@Param("idFranchisee") String idFranchisee, @Param("today") LocalDateTime today);

    /**
     * todo 条件限定
     * @param regionCode
     * @return
     */
    @Query("select t from PromotionEntity t where t.regionCode=:regionCode and t.enabled =1 and :today between t.effectiveTime and t.expiredTime ORDER BY t.createTime DESC")
    List<PromotionEntity> findByRegionCode(@Param("regionCode") String regionCode, @Param("today") LocalDateTime today);

    /**
     * 获得当前加盟商的活动
     * @param idFranchisee
     * @return
     */
    @Query("SELECT t FROM PromotionEntity t WHERE t.idFranchisee = :idFranchisee ORDER BY t.createTime DESC")
    Page<PromotionEntity> findPromotionEntityByIdFranchisee(@Param("idFranchisee") String idFranchisee, PageRequest pageRequest);

    /**
     * 获得当前加盟商的活动
     * @param idFranchisee
     * @return
     */
    @Query("select t from PromotionEntity t " +
            "where " +
            "t.idFranchisee=:idFranchisee " +
            "and t.effectiveTime >= :startDate and t.effectiveTime <=:endDate " +
            "and t.popFlg =:popFlg " +
            "and t.enabled =:enabled " +
            "ORDER BY t.createTime DESC" )
    List<PromotionEntity> findByIdFranchisee(@Param("idFranchisee") String idFranchisee,
                                             @Param("startDate") LocalDateTime startDate,
                                             @Param("endDate") LocalDateTime endDate,
                                             @Param("popFlg") Integer popFlg,
                                             @Param("enabled") Integer enabled);

    /**
     * 获得当前加盟商的活动
     * @param idFranchisee
     * @return
     */
//    @Query(value = "select * from sys_promotion t where t.id_franchisee=:idFranchisee " +
//            "and if(ifnull(:popFlg,'')!= '', t.pop_flg = :popFlg, 1=1)" +
//            "and if(ifnull(:enabled,'')!= '', t.enabled = :enabled, 1=1) " +
//            "ORDER BY t.create_time DESC", nativeQuery = true )
//    List<PromotionEntity> findByIdFranchisee(@Param("idFranchisee") String idFranchisee,
//                                             @Param("popFlg") Integer popFlg,
//                                             @Param("enabled") Integer enabled);

}